<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="病人姓名" prop="patientName">
        <el-input v-model="queryParams.patientName" placeholder="请输入病人姓名" clearable />
      </el-form-item>
      <el-form-item label="检查日期" prop="studyDate">
        <el-date-picker
          v-model="queryParams.studyDate"
          type="date"
          placeholder="选择日期"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      <el-form-item label="设备类型" prop="modality">
        <el-input v-model="queryParams.modality" placeholder="请输入设备类型" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 上传区域 -->
    <el-upload
      class="upload-demo"
      :action="uploadUrl"
      :before-upload="beforeUpload"
      :on-success="handleUploadSuccess"
      :on-error="handleUploadError"
      :show-file-list="false"
      accept=".zip"
    >
      <el-button type="primary">上传数据包</el-button>
    </el-upload>

    <!-- 表格区域 -->
    <el-table v-loading="loading" :data="worklistData">
      <el-table-column label="部门名称" align="center" prop="deptName" />
      <el-table-column label="病人姓名" align="center" prop="patientName" />
      <el-table-column label="设备类型" align="center" prop="modality" />
      <el-table-column label="检查日期" align="center" prop="studyDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.studyDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 上传进度对话框 -->
    <el-dialog
      title="处理进度"
      v-model="progressVisible"
      :close-on-click-modal="false"
      :show-close="false"
      width="30%"
    >
      <el-progress :percentage="uploadProgress" />
      <div style="text-align: center; margin-top: 10px">{{ progressText }}</div>
    </el-dialog>
  </div>
</template>

<script setup name="Worklist">
import { ref, onMounted } from 'vue';
import { listWorklist, uploadZipFile } from '@/api/pacs/worklist';
import { ElMessage } from 'element-plus';

const loading = ref(false);
const total = ref(0);
const worklistData = ref([]);
const progressVisible = ref(false);
const uploadProgress = ref(0);
const progressText = ref('正在处理数据包...');
const uploadUrl = import.meta.env.VITE_APP_BASE_API + '/pacs/worklist/upload';

const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  patientName: undefined,
  studyDate: undefined,
  modality: undefined
});

// 查询列表
function getList() {
  loading.value = true;
  listWorklist(queryParams.value).then(response => {
    worklistData.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 搜索按钮操作
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

// 重置按钮操作
function resetQuery() {
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
    patientName: undefined,
    studyDate: undefined,
    modality: undefined
  };
  handleQuery();
}

// 上传前校验
function beforeUpload(file) {
  const isZip = file.type === 'application/x-zip-compressed' || file.type === 'application/zip';
  if (!isZip) {
    ElMessage.error('只能上传ZIP格式文件！');
    return false;
  }
  progressVisible.value = true;
  uploadProgress.value = 0;
  return true;
}

// 上传成功回调
function handleUploadSuccess(response) {
  if (response.code === 200) {
    ElMessage.success('上传成功');
    getList(); // 刷新列表
  } else {
    ElMessage.error(response.msg || '上传失败');
  }
  progressVisible.value = false;
}

// 上传失败回调
function handleUploadError() {
  ElMessage.error('上传失败');
  progressVisible.value = false;
}

onMounted(() => {
  getList();
});
</script>
